package com.hbnu.web;

import com.hbnu.entity.User;
import com.hbnu.enums.Status;
import com.hbnu.service.FunctionService;
import com.hbnu.service.UserService;
import java.io.*;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import static com.hbnu.tools.PrintTool.*;
import static com.hbnu.tools.ScannerTool.nextLine;

public class Manage {
    private FunctionService functionService;
    private final UserService userService =new UserService();
    public static User user;
    public static StringBuilder tip;
    public static Map<Integer,User> userMap=new HashMap<>();


    public void start(){
        initPrint();
        logIn();
        while (true){
            try {
                //已去除前后空白
                String line = nextLine(tip);
                String[] words = line.split("\\s+");
                //根据长度查找
                if (functionService.cmdSet.contains(words[0])) {
                    switch (words.length) {
                        case 1 -> {
                            Runnable function = functionService.cmdMap.get(words[0]);
                            if (null != function) {
                                function.run();
                            }
                        }
                        case 2 -> {
                            Consumer<String> function = functionService.oneMap.get(words[0]);
                            if (null != function) {
                                function.accept(words[1]);
                            }
                        }
                        default -> {
                            BiConsumer<String, String> function = functionService.twoMap.get(words[0]);
                            if (null != function) {
                                function.accept(words[1], words[2]);
                            }
                        }
                    }
                }
                //命令不存在
                else {
                    error(line, Status.NO_CMD);
                }
            }catch (Exception e){
                println(e.getMessage());
            }
        }

    }

    void initPrint(){
        println("Welcome to the " + Status.LOGO +" monitor!\n");
        println("      _    _ ____  _   _ _    _            .-~~~-.\n" +
                "     | |  | |  _ \\| \\ | | |  | |     .-~~'     __  `~~-.\n" +
                "     | |__| | |_) |  \\| | |  | |    /       .-~  ~-.     \\\n" +
                "     |  __  |  _ <| . ` | |  | |   |     .-'        `-.   |\n" +
                "     | |  | | |_) | |\\  | |__| |    \\   /              \\ /\n" +
                "     |_|  |_|____/|_| \\_|\\____/      `-.~ ._ .,., ,..~-'\n");
        println("Copyright (c) Q and/or its affiliates.[version: "+ Status.Version+"]");
    }
    void logIn(){
        while (true) {
            print("login: ");
            String userName = nextLine();
            if(userName.isEmpty()) continue;
            //获得了用户名
            String password = getPassword();
            if(password.isEmpty()) {
                logonError(Status.NULL_PASS);
                continue;
            }
            //登录成功，获取用户类型
            if("root".equals(userName)&&"root".equals(password)) {
                user=new User();
                user.setName(userName);
                //登录成功,初始化
                logonInit();
                //更新登录时间
                break;
            }
            println("用户不存在！");
        }
        welcome();
    }
    void welcome(){
        println("Your login time is "+getTime(LocalDateTime.now()));
        println("");
        println("\t\t(^_^): "+printGreeting()+ user.getName()+"!");
        println("");
    }

    //获取密码，在控制台隐藏状态
    String getPassword(){
        String password;
        print("Password: ");
        Console console = System.console();
        if (console != null) {
            char[] passwordArray = console.readPassword();
            password = new String(passwordArray);
        }else password = nextLine();

        return password;
    }
    //数据初始化
    void logonInit(){
        //获取所有用户
        List<User> userList=userService.selectUserAll();
        int i=1;
        for (User u : userList) {
            userMap.put(i++,u);
        }
        tip =new StringBuilder();
        tip.append("[").append("HBNU").append("@").append(user.getName()).append(" ");
        tip.append("~").append("]").append("# ");
        //
        functionService=new FunctionService();
    }
}
